﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MultiResolutioning.Structures;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;

namespace MultiResolutioning.Data
{
	/// <summary>
	/// Klasa reprezentuje siatke powierzchni trojwymiarowej.
	/// </summary>
	public class MeshData
	{
		private int vertexMaxId = 0;
		private int faceMaxId = 0;
		public List<Vertex> vertices;
		public List<Face> faces;
		public List<string> vertexPropertiesNames;

		/// <summary>
		/// Generuje nowy id wierzcholka.
		/// </summary>
		/// <returns></returns>
		public int GetNewVertexId()
		{
			return this.vertexMaxId++;
		}

		/// <summary>
		/// Generuje nowy id trojkata.
		/// </summary>
		/// <returns></returns>
		public int GetNewFaceId()
		{
			return this.faceMaxId++;
		}

		/// <summary>
		/// Usuwa trojkat.
		/// </summary>
		/// <param name="toRemove">Element do usuniecia.</param>
		public void RemoveFace(Face toRemove)
		{
			faces.Remove(toRemove);
			foreach (Vertex v in vertices)
			{
				v.faces.Remove(toRemove);
			}
		}

		/// <summary>
		/// Usuwa wszystkie trojkaty z flaga removed rowna true.
		/// </summary>
		public void RemoveFaces()
		{
			this.faces.RemoveAll(x => x.removed);
			foreach (Vertex v in this.vertices)
			{
				v.faces.RemoveAll(x => x.removed);
			}
		}

		/// <summary>
		/// Usuwa wierzcholek
		/// </summary>
		/// <param name="toRemove">Element do usuniecia.</param>
		public void RemoveVertex(Vertex toRemove)
		{
			foreach (Vertex v in vertices)
			{
				v.neighbors.Remove(toRemove);
			}

			foreach (Vertex v in vertices)
			{
				int f = v.faces.RemoveAll(x => x.vertices.Contains(toRemove));
				f++;
			}

			vertices.Remove(toRemove);
		}

		/// <summary>
		/// Usuwa wszystkie wierzcholki oznaczone flaga removed na true.
		/// </summary>
		public void RemoveVertices()
		{
			vertices.RemoveAll(x => x.removed);
		}

	}
}
